<?php

namespace App\Http\Controllers\Docs;

use App\Models\DocDetail;
use App\Models\DocInfo;
use App\Models\DocList;
use App\Models\DocVersion;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class DocsInfoController extends Controller
{
    /** 文档详情 */
    public function index(Request $request)
    {
        $doc_id = $request->get('doc_id');

        /** 文档版本 */
        $doc_version = DocVersion::where(compact('doc_id'))->orderBy('created_at', 'desc')->pluck('version', 'id')->toArray();
        $version_id  = $request->get('version_id') ?? key($doc_version);

        /** 目录 */
        $doc_list = DocList::plumb(DocList::where(['version_id' => $version_id])->get()->toArray(), 0, '');
        /** 详情 */
        $list_id = DocList::firstChild($version_id, $request->get('list_id'));
        $doc_detail = DocDetail::where(compact('list_id'))->first();

        return view('doc.info.index', compact('doc_id', 'version_id', 'list_id', 'doc_version', 'doc_list', 'doc_detail'));
    }

    /** 添加版本 */
    public function addVersion(Request $request)
    {
        $doc_id = $request->doc_id;
        $model  = empty($id) ? (new DocVersion()) : (DocVersion::find($id));
        if ($request->isMethod('post')) {
            $model->doc_id  = $request->doc_id;
            $model->version = $request->version;
            if ($model->save()) {
                return $this->success('保存成功!');
            }
        }
        return view('doc.info.addVersion', ['doc_id' => $doc_id]);
    }

    /** 添加目录 */
    public function addList(Request $request)
    {
        $id         = $request->id;
        $version_id = $request->version_id;
        $pid        = $request->pid;
        $model      = empty($id) ? (new DocList()) : (DocList::find($id));
        if ($request->isMethod('post')) {
            $model->pid        = $request->pid ?? 0;
            $model->version_id = $request->version_id;
            $model->name       = $request->name;

            if ($model->save()) {
                return $this->success('保存成功!');
            }
        }
        $topList = DocList::where('pid', 0)->orderBy('sort', 'asc')->pluck('name', 'id')->toArray();
        return view('doc.info.addList', ['topList' => $topList, 'model'=>$model,'pid' => $pid, 'version_id' => $version_id]);
    }

    /** 编辑文档内容 */
    public function editDetail(Request $request)
    {
        $id         = $request->get('list_id');
        $version_id = $request->version_id;
        $pid        = $request->pid;

        $model = empty($id) ? (new DocList()) : (DocList::find($id));
        if ($request->isMethod('post')) {
            $data              = $request->all();
            $model->name       = $data['name'];
            $model->version_id = $data['version_id'];
            $model->pid        = $data['pid'];
            $model->save();
            DocDetail::updateOrCreate(['list_id' => $model->id], ['list_id' => $model->id, 'content' => $data['content']]);
            return $this->success('操作成功!','0',['id'=>$model->id]);
        }

        return view('doc.info.editDetail', compact('model', 'version_id', 'pid'));
    }

    /** 删除内容 */
    public function delDetail(Request $request)
    {
        $id = $request->get('id');
        DocDetail::where('list_id',$id)->delete();
        DocList::where('id',$id)->delete();
        return $this->success('删除成功!');
    }
}
